package com.txd.mofa.adapter;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.txd.mofa.R;

import java.util.Map;

public class ShouyeAdapter extends BaseQuickAdapter<Map<String, String>, BaseViewHolder> {

    private String s = "999";

    public ShouyeAdapter(int layoutResId) {
        super(layoutResId);
    }

    public void setType(String s){

        this.s = s;
    }

    @Override
    protected void convert(BaseViewHolder helper, Map<String, String> item) {

        ImageView iv = helper.getView(R.id.iv);
        helper.addOnClickListener(R.id.iv_sc);
        helper.addOnClickListener(R.id.iv_qiehuan);

        //fallback( R.drawable.blank) //url为空的时候,显示的图片
        Glide.with(mContext).load(item.get("img"))
                .placeholder(R.mipmap.defaulttu)////加载成功前显示的图片
                .thumbnail(0.3f)
                .dontAnimate()
                .into(iv);

        helper.setText(R.id.tv,item.get("name"));

        DisplayMetrics dm2 = mContext.getResources().getDisplayMetrics();

//        Log.e("weight",item.get("weight"));
//        Log.e("height",item.get("height"));

        if (item.get("weight") != null && item.get("height") != null) {
            float scale = Float.parseFloat(item.get("weight").trim()) / Float.parseFloat(item.get("height").trim());

            int imageHeight = (int) ((dm2.widthPixels / 2 - 24) / scale);


//        iv.setMaxHeight(imageHeight);

//        LinearLayout ll = helper.getView(R.id.ll);

            ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) iv.getLayoutParams();
            lp.height = imageHeight;//设置图片高度

        }
//        iv.setScaleType(ImageView.ScaleType.FIT_XY);

        ImageView iv_sc = helper.getView(R.id.iv_sc);
        ImageView iv_qiehuan = helper.getView(R.id.iv_qiehuan);

        if (s.equals("2")){
            iv_sc.setVisibility(View.GONE);
            iv_qiehuan.setVisibility(View.GONE);
        }else {
            iv_sc.setVisibility(View.VISIBLE);
            iv_qiehuan.setVisibility(View.VISIBLE);
        }

        if (item.get("collect") != null) {
            if (item.get("collect").equals("2")) {
                Glide.with(mContext).load(R.mipmap.sc).into((ImageView) helper.getView(R.id.iv_sc));
            } else {
                Glide.with(mContext).load(R.mipmap.sc_xuan).into((ImageView) helper.getView(R.id.iv_sc));
            }
        }
//
        if (item.get("qiehuan") != null) {
            if (item.get("qiehuan").equals("111")) {
                Glide.with(mContext).load(R.mipmap.tihuan_xuan).into((ImageView) helper.getView(R.id.iv_qiehuan));
            } else {
                Glide.with(mContext).load(R.mipmap.tihuan).into((ImageView) helper.getView(R.id.iv_qiehuan));
            }
        }else {
            Glide.with(mContext).load(R.mipmap.tihuan).into((ImageView) helper.getView(R.id.iv_qiehuan));
        }
//
    }
}
